﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;

namespace Engine.Sgml.Parser {
    internal sealed class NameReader {
        private StringBuilder builder = new StringBuilder();

        public string ReadNameToken(SgmlStreamReader reader) {
            Debug.Assert(reader != null);
            if (XmlConvert.IsStartNCNameChar(reader.CurrentChar)) {
                builder.Clear();
                builder.Append(reader.CurrentChar);
                while (XmlConvert.IsNCNameChar(reader.ReadChar())) {
                    builder.Append(reader.CurrentChar);
                    }
                return builder.ToString();
                }
            return null;
            }

        public string ScanNameToken(SgmlStreamReader reader) {
            Debug.Assert(reader != null);
            do {
                if (XmlConvert.IsStartNCNameChar(reader.CurrentChar)) {
                    builder.Clear();
                    builder.Append(reader.CurrentChar);
                    while (XmlConvert.IsNCNameChar(reader.ReadChar())) {
                        builder.Append(reader.CurrentChar);
                        }
                    return builder.ToString();
                    }
                reader.ReadChar();
                } while (reader.CurrentChar != Characters.EndOfFile);
            return null;
            }

        public string ReadPrefixNameToken(SgmlStreamReader reader) {
            Debug.Assert(reader != null);
            if (XmlConvert.IsStartNCNameChar(reader.CurrentChar)) {
                builder.Clear();
                builder.Append(reader.CurrentChar);
                while (XmlConvert.IsNCNameChar(reader.ReadChar())) {
                    builder.Append(reader.CurrentChar);
                    }
                if (reader.CurrentChar == Characters.EndNamePrefix) {
                    builder.Append(reader.CurrentChar);
                    if (XmlConvert.IsStartNCNameChar(reader.ReadChar())) {
                        builder.Append(reader.CurrentChar);
                        while (XmlConvert.IsNCNameChar(reader.ReadChar())) {
                            builder.Append(reader.CurrentChar);
                            }
                        }
                    }
                return builder.ToString();
                }
            return null;
            }

        public string ScanPrefixNameToken(SgmlStreamReader reader) {
            Debug.Assert(reader != null);
            bool hasPrefix = false;
            builder.Clear();
            do {
                if (XmlConvert.IsStartNCNameChar(reader.CurrentChar)) {
                    builder.Append(reader.CurrentChar);
                    while (XmlConvert.IsNCNameChar(reader.ReadChar())) {
                        builder.Append(reader.CurrentChar);
                        }
                    if (reader.CurrentChar == Characters.EndNamePrefix && !hasPrefix) {
                        builder.Append(reader.CurrentChar);
                        hasPrefix = true;
                        }
                    else
                        break;
                    }
                reader.ReadChar();
                } while (reader.CurrentChar != Characters.EndOfFile);
            if (builder.Length > 0)
                return builder.ToString();
            else
                return null;
            }
        }
    }
